package team.szm.scd.database.typehandler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.DayOfWeek;

@MappedTypes(DayOfWeek.class)
@MappedJdbcTypes(JdbcType.INTEGER)
public class DayOfWeekTypeHandler extends BaseTypeHandler<DayOfWeek> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, DayOfWeek dayOfWeek, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, dayOfWeek.getValue());
    }

    @Override
    public DayOfWeek getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return DayOfWeek.of(Integer.parseInt(resultSet.getString(s)));
    }

    @Override
    public DayOfWeek getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return DayOfWeek.of(resultSet.getInt(i));
    }

    @Override
    public DayOfWeek getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return DayOfWeek.of(callableStatement.getInt(i));
    }
}
